import { initGlobalState } from 'qiankun';
import { getToken } from '../utils/cookies';

// 全局状态管理
const initialState = {
  user: {
    id: "",
    name: "",
    nickname: "",
    username: "",
    avatar: "",
    role: "",
    permissions: []
  },
  token: getToken() || "",
  theme: "light",
  language: "zh-CN",
  currentApp: "home",
  publicPath: "/wocwin-admin/",
  layout: "vertical",
  isCollapse: false,
  breadcrumb: true,
  breadcrumbIcon: true,
  tabs: true,
  tabsIcon: false,
  isWatermark: true,
  primaryColor: "#0052D9", // 使用统一的默认主题色
  menuList: [] // 添加菜单列表
};

const actions = initGlobalState(initialState);

// 监听全局状态变化
actions.onGlobalStateChange((state, prev) => {
  console.log("[GlobalState] Global state changed:", state, prev);
  // 更新currentState
  currentState = { ...currentState, ...state };
});

// 添加获取全局状态的方法
let currentState = initialState;
(actions as any).getGlobalState = () => {
  return currentState;
};

// 重写setGlobalState方法以更新currentState
const originalSetGlobalState = actions.setGlobalState;
actions.setGlobalState = (state: any) => {
  currentState = { ...currentState, ...state };
  return originalSetGlobalState(state);
};

export default actions;
